Skip to content

fix: retry failed jobs with solid queue's own transactional retry - #53

Merged
vishaltps merged 1 commit into
vishaltps:mainfrom
phmunhoz:fix/retry-failed-job-atomic
Nov 28, 2026
Merged

vishaltps merged 1 commit into
vishaltps:mainfrom
phmunhoz:fix/retry-failed-job-atomic

Conversation

@phmunhoz

Copy link
Copy Markdown
Contributor

Problem

Retrying a failed job could leave it with no execution at all: no ready, claimed, failed or scheduled execution, and no finished_at. Solid Queue never runs it again. The dashboard lists it under the "Pending" status filter forever, and no Overview card counts it.

FailedJobService#retry_job reimplemented the retry by hand:

ActiveRecord::Base.transaction do
  SolidQueue::ReadyExecution.create!(...)
  failed_execution.destroy!
end

ActiveRecord::Base.transaction opens a transaction on the app's primary database. When Solid Queue has its own database (config.solid_queue.connects_to, the Rails 8 default), that transaction doesn't cover the queue tables, so the two writes commit separately.

How it happened

I found an orphaned job in a Rails 8 app (MySQL, separate queue database, solid_queue 1.7.0). Its development log shows the full sequence for one click on Retry:

  1. The monitor inserts the ready execution, which commits immediately on the queue database.
  2. A worker claims the job and runs it, and the job fails again (ActiveJob::DeserializationError).
  3. Solid Queue's Job#failed_with tries to create a new failed execution and hits the unique index on job_id, because the old one still exists. It handles this case on purpose (rescue ActiveRecord::RecordNotUnique) by updating the existing failed execution with the new error.
  4. The monitor, which loaded that failed execution before step 1, now deletes it.

The job is left with no execution of any kind. Solid Queue handled its own race correctly; it just can't account for an outside tool deleting a row in the middle of it.

Fix

retry_job now calls Solid Queue's own FailedExecution#retry, the same method Solid Queue and Mission Control use:

with_lock do
  job.reset_execution_counters
  job.prepare_for_execution
  destroy!
end

That runs as one locked transaction on Solid Queue's own connection. FailedExecution#retry exists and is transactional in every published solid_queue version I checked (0.1.1 through 1.7.2), so the gemspec's solid_queue >= 0.1.0 stays valid.

Side effects, all matching a retry done by Solid Queue itself:

  • Execution counters are reset, so retry_on ... attempts: starts over after a manual retry.
  • limits_concurrency is respected. The old code created a ready execution directly, skipping the concurrency check. Now a retried job whose slot is taken goes to blocked executions, so the service's success message changed from "Job moved to ready queue for retry" to "Job queued for retry".
  • A failed execution retried or discarded by someone else after it was loaded makes with_lock raise RecordNotFound. That is now returned as { success: false, message: 'Failed job not found' } instead of raising.
  • Bulk retry (retry_all) catches errors per job, the same way DiscardReadyJobService#discard_many does. A lock timeout or deadlock on one job counts as failed and the rest are still retried, instead of stopping halfway with an error page.

Tests

  • New spec/services/solid_queue_monitor/failed_job_service_spec.rb covers: a normal retry; delegation to FailedExecution#retry; a missing failed execution; one removed after it was loaded; a missing job; and bulk retry, including partial failure and a job whose retry raises.
  • The test stand-in for FailedExecution#retry in spec/support/solid_queue_stubs.rb now behaves like the real one: one with_lock transaction that creates the ready execution and deletes the failed one, raising RecordNotFound if the row is gone.
  • Two of the new specs fail on the old code.
  • The suite runs against these stand-in models, not the real gem, so the counter reset and concurrency behavior are covered by reading Solid Queue's source, not by tests.

bundle exec rspec: 384 examples, 0 failures. bundle exec rubocop: no offenses.

Not in this PR

  • Discard on failed jobs, Execute and Reject on scheduled jobs use the same ActiveRecord::Base.transaction around queue-database writes. I'd like to fix them in a follow-up PR. Discard needs a decision first: Solid Queue's own FailedExecution#discard deletes the job, while the monitor marks it finished (finished_at), and that also ignores SolidQueue.preserve_finished_jobs = false. Which behavior would you prefer?
  • The per-row Retry/Discard flash always reports success. The controller checks if service.retry_job(id), and the service returns a Hash, which is always truthy. I have a separate small fix ready for that.

@vishaltps
vishaltps merged commit aff54eb into vishaltps:main Nov 28, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants